package com.ericom.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
public class Product extends BaseObject {
	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).append("name", name)
				.append("description", description).toString();
	}
	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof Product))
			return false;
		Product castOther = (Product) other;
		return new EqualsBuilder().append(id, castOther.id)
				.append(name, castOther.name)
				.append(description, castOther.description).isEquals();
	}
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(id).append(name)
				.append(description).toHashCode();
	}
	@OneToMany(mappedBy="product",cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=ProductComment.class)
	public Set<ProductComment> getProductComments() {
		return productComments;
	}
	public void setProductComments(Set<ProductComment> productComments) {
		this.productComments = productComments;
	}
	
	
	private Long id;
	private String name;
	private String description;
	private String longDesc;
	private String thumb;
	private String image;
	private String validityFrom;
	private String validityTo;
	private int online;
	private int instock;
	private String tags;
	private Set<Category> categories=new HashSet<Category>();
	private Set<Supplier> suppliers=new HashSet<Supplier>();
	private Set<Product> bindProducts=new HashSet<Product>();
	private Set<Price> prices=new HashSet<Price>();
	private Set<VariationType> variationTypes=new HashSet<VariationType>();
	private Set<ProductComment> productComments=new HashSet<ProductComment>();
	
	
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="product_varitaiont_type",joinColumns={@JoinColumn(name="prod_id")},inverseJoinColumns=@JoinColumn(name="vt_id"))
	public Set<VariationType> getVariationTypes() {
		return variationTypes;
	}
	public void setVariationTypes(Set<VariationType> variationTypes) {
		this.variationTypes = variationTypes;
	}
	
	private Brand brand;
	@ManyToOne(cascade = { CascadeType.MERGE })  
	@JoinColumn(name = "brandId")
	public Brand getBrand() {
		return brand;
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	@OneToMany(mappedBy="product",cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=Price.class)
	public Set<Price> getPrices() {
		return prices;
	}
	
	public void setPrices(Set<Price> prices) {
		this.prices = prices;
	}

	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="product_bind",joinColumns={@JoinColumn(name="prod_id")},inverseJoinColumns=@JoinColumn(name="bind_id"))
	public Set<Product> getBindProducts() {
		return bindProducts;
	}

	public void setBindProducts(Set<Product> bindProducts) {
		this.bindProducts = bindProducts;
	}

	@Id@GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLongDesc() {
		return longDesc;
	}

	public void setLongDesc(String longDesc) {
		this.longDesc = longDesc;
	}

	public String getThumb() {
		return thumb;
	}

	public void setThumb(String thumb) {
		this.thumb = thumb;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getValidityFrom() {
		return validityFrom;
	}

	public void setValidityFrom(String validityFrom) {
		this.validityFrom = validityFrom;
	}

	public String getValidityTo() {
		return validityTo;
	}

	public void setValidityTo(String validityTo) {
		this.validityTo = validityTo;
	}

	public int getOnline() {
		return online;
	}

	public void setOnline(int online) {
		this.online = online;
	}

	public int getInstock() {
		return instock;
	}

	public void setInstock(int instock) {
		this.instock = instock;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="product_category",joinColumns={@JoinColumn(name="prod_id")},inverseJoinColumns=@JoinColumn(name="cat_id"))
	public Set<Category> getCategories() {
		return categories;
	}

	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="product_supplier",joinColumns={@JoinColumn(name="prod_id")},inverseJoinColumns=@JoinColumn(name="sup_id"))
	public Set<Supplier> getSuppliers() {
		return suppliers;
	}

	public void setSuppliers(Set<Supplier> suppliers) {
		this.suppliers = suppliers;
	}

}
